﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class OrderPartHelpEditControl : UserControl
    {
        private IOrderPartHelp _orderPartHelp;
        private Workspace _workspace;

        public OrderPartHelpEditControl(IOrderPartHelp _orderPartHelp, Workspace workspace)
        {
            _workspace = workspace;

            cbxParentPart.DataSource = workspace.GetAllOrderPartHelps();

            OrderPartHelp = _orderPartHelp;
            InitializeComponent();
        }

        public IOrderPartHelp OrderPartHelp
        {
            get { return _orderPartHelp; }
            set
            {
                _orderPartHelp = value;

                cbxParentPart.SelectedItem = _orderPartHelp != null ? _orderPartHelp.ParentPart : null;
                tbxName.Text = _orderPartHelp != null ? _orderPartHelp.Name : string.Empty;
                tbxDescription.Text = _orderPartHelp != null ? _orderPartHelp.Description : string.Empty;
            }
        }

        private void cbxParentPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_orderPartHelp == null)
                throw new InvalidOperationException();

            _orderPartHelp.ParentPart = (IOrderPartHelp)cbxParentPart.SelectedItem;
        }

        private void tbxName_TextChanged(object sender, EventArgs e)
        {
            if (_orderPartHelp == null)
                throw new InvalidOperationException();

            _orderPartHelp.Name = tbxName.Text;
        }

        private void tbxDescription_TextChanged(object sender, EventArgs e)
        {
            if (_orderPartHelp == null)
                throw new InvalidOperationException();

            _orderPartHelp.Description = tbxDescription.Text;
        }
    }
}
